package com.atguigu.tingshu.order.template.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.template.OrderConfirmTemplate;
import com.atguigu.tingshu.order.template.TemplateBean;
import com.atguigu.tingshu.user.client.VipServiceConfigFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Arrays;

@TemplateBean(type = SystemConstant.ORDER_ITEM_TYPE_VIP)
public class VipOrderConfirm extends OrderConfirmTemplate {

    @Autowired
    private VipServiceConfigFeignClient vipServiceConfigFeignClient;

    @Override
    public void trade(OrderInfoVo orderInfoVo, TradeVo tradeVo) {
        // 根据已选择的vip套餐信息
        Result<VipServiceConfig> vipConfigResult = vipServiceConfigFeignClient.findVipInfoById(tradeVo.getItemId());
        Assert.notNull(vipConfigResult, "该vip套餐信息不存在");
        VipServiceConfig vipServiceConfig = vipConfigResult.getData();
        // 计算vip价格信息
        BigDecimal originalAmount = vipServiceConfig.getPrice();
        BigDecimal orderAmount = vipServiceConfig.getDiscountPrice();
        BigDecimal derateAmount = originalAmount.subtract(orderAmount);
        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setOrderAmount(orderAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        // 生成订单明细列表
        orderInfoVo.setOrderDetailVoList(
                Arrays.asList(OrderDetailVo.builder()
                        .itemId(tradeVo.getItemId())
                        .itemName("vip会员" + vipServiceConfig.getName())
                        .itemPrice(orderAmount)
                        .itemUrl(vipServiceConfig.getImageUrl())
                        .build())
        );
        // 生成订单减免列表
        orderInfoVo.setOrderDerateVoList(
                Arrays.asList(OrderDerateVo.builder()
                        .derateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT)
                        .derateAmount(derateAmount)
                        .remarks("VIP服务折扣")
                        .build())
        );
    }
}
